package com.tm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.model.dtos.BehaviorDto;
import com.tm.media.model.entity.ApUserEntity;
import com.tm.media.model.entity.ApUserFanEntity;
import com.tm.media.model.entity.StatisticsEntity;
import com.tm.media.model.mapper.ApUserFanMapper;
import com.tm.media.model.mapper.ApUserMapper;
import com.tm.media.model.mapper.StatisticsMapper;
import com.tm.media.utils.utils.Result;
import com.tm.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


/**
 * @Author Ly
 * @Data 2023/2/25 17:06
 * @ClassName StatisticsServiceImpl
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<StatisticsMapper, StatisticsEntity> implements ApUserService {
@Autowired
private StatisticsMapper statisticsMapper;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ApUserFanMapper apUserFanMapper;
@Autowired
private ApUserMapper apUserMapper;

    /**
     * 关注
     * @param behaviorDto
     * @return
     */
    @Override
    public Result user_follow(BehaviorDto behaviorDto) {
        //文章ID是4
        QueryWrapper<ApUserFanEntity> queryWrapper = new QueryWrapper<>();
        //假设app用户的ID是1
        queryWrapper.eq("fans_id",1);
        queryWrapper.eq("user_id",behaviorDto.getAuthor_id());
        behaviorDto.setEntry_id(behaviorDto.getArticle_id());
        ApUserFanEntity apUserFanEntity = apUserFanMapper.selectOne(queryWrapper);
        //查询出来图文数据表
        behaviorDto.setEntry_id(behaviorDto.getArticle_id());
        //查询出来文章ID对应的图文数据表
        StatisticsEntity statistics= statisticsMapper.selectStatistics(behaviorDto);

           return   guanzhu(apUserFanEntity,statistics);
    }

    /**
     * 关注
     * @param apUserFanEntity
     * @param statistics
     * @return
     */
    public Result guanzhu(ApUserFanEntity apUserFanEntity, StatisticsEntity statistics){
            if (apUserFanEntity!=null){
                statistics.setFollow(statistics.getFollow()-1);
                redisTemplate.opsForValue().decrement("关注"+statistics.getArticle(),1);
                //删除关注的信息
                //看一下当前用户的姓名
                apUserFanMapper.deleteById(apUserFanEntity.getId());
            }else {
                statistics.setFollow(statistics.getFollow()+1);
                redisTemplate.opsForValue().set("关注"+statistics.getArticle(),1);
                //新增关注表
                ApUserFanEntity userFanEntity = new ApUserFanEntity();
                userFanEntity.setUserId(statistics.getUserId());
                userFanEntity.setFansId(1);
                userFanEntity.setLevel(0);
                //看一下当前用户的姓名
                QueryWrapper<ApUserEntity> apUserQueryWarpe = new QueryWrapper<>();
                apUserQueryWarpe.eq("id",1);
                ApUserEntity apUserEntity = apUserMapper.selectOne(apUserQueryWarpe);
                userFanEntity.setFansName(apUserEntity.getName());
                apUserFanMapper.insert(userFanEntity);
            }
            statisticsMapper.updateById(statistics);
            return Result.SUCCESS(200,"成功");
    }


}
